core: Clean up stale packfiles
authorColin Walters <walters@verbum.org>
Wed, 4 Apr 2012 03:45:28 +0000 (23:45 -0400)
committerColin Walters <walters@verbum.org>
Wed, 4 Apr 2012 03:46:34 +0000 (23:46 -0400)
src/libostree/ostree-repo.c
src/libostree/ostree-repo.h

index 04e9f974681cf581fa12adea6984b985cd4f264d..849af010cde0013816227e451e6e221e3ebd9cf5 100644 (file)
@@ -1839,6 +1839,7 @@ ostree_repo_resync_cached_remote_pack_indexes (OstreeRepo       *self,
   GFile *cache_path = NULL;
   GFile *superindex_cache_path = NULL;
   GPtrArray *index_files = NULL;
+  GPtrArray *data_files = NULL;
   GHashTable *new_pack_indexes = NULL;
   GHashTableIter hash_iter;
   gpointer key, value;
@@ -1919,6 +1920,28 @@ ostree_repo_resync_cached_remote_pack_indexes (OstreeRepo       *self,
   superindex_cache_path = g_file_get_child (cache_path, "index");
   if (!ot_util_variant_save (superindex_cache_path, superindex_variant, cancellable, error))
     goto out;
+
+  /* Now also delete stale pack files */
+
+  if (!list_files_in_dir_matching (cache_path,
+                                   "ostpack-", ".data",
+                                   &data_files, 
+                                   cancellable, error))
+    goto out;
+  
+  for (i = 0; i < data_files->len; i++)
+    {
+      GFile *data_file = data_files->pdata[i];
+
+      g_free (pack_checksum);
+      pack_checksum = get_checksum_from_pack_name (ot_gfile_get_basename_cached (data_file));
+
+      if (!g_hash_table_lookup (new_pack_indexes, pack_checksum))
+        {
+          if (!ot_gfile_unlink (data_file, cancellable, error))
+            goto out;
+        }
+    }
       
   ret = TRUE;
   ot_transfer_out_value (out_cached_indexes, &ret_cached_indexes);
@@ -1936,6 +1959,41 @@ ostree_repo_resync_cached_remote_pack_indexes (OstreeRepo       *self,
   return ret;
 }
 
+gboolean
+ostree_repo_clean_cached_remote_pack_data (OstreeRepo       *self,
+                                           const char       *remote_name,
+                                           GCancellable     *cancellable,
+                                           GError          **error)
+{
+  gboolean ret = FALSE;
+  GFile *cache_path = NULL;
+  GPtrArray *data_files = NULL;
+  guint i;
+
+  if (!ensure_remote_cache_dir (self, remote_name, &cache_path, cancellable, error))
+    goto out;
+
+  if (!list_files_in_dir_matching (cache_path,
+                                   "ostpack-", ".data",
+                                   &data_files, 
+                                   cancellable, error))
+    goto out;
+
+  for (i = 0; i < data_files->len; i++)
+    {
+      GFile *data_file = data_files->pdata[i];
+      
+      if (!ot_gfile_unlink (data_file, cancellable, error))
+        goto out;
+    }
+
+  ret = TRUE;
+ out:
+  g_clear_object (&cache_path);
+  ot_clear_ptrarray (&data_files);
+  return ret;
+}
+
 /**
  * Load the index for pack @pack_checksum from cache directory for
  * @remote_name.
index a4244962f7aa053e3c66fe4572577c917ea2a88a..05a916df7dd09da5759f3ec7444322218286750a 100644 (file)
@@ -248,6 +248,11 @@ gboolean     ostree_repo_resync_cached_remote_pack_indexes (OstreeRepo       *se
                                                             GCancellable     *cancellable,
                                                             GError          **error);
 
+gboolean     ostree_repo_clean_cached_remote_pack_data (OstreeRepo       *self,
+                                                        const char       *remote_name,
+                                                        GCancellable     *cancellable,
+                                                        GError          **error);
+
 gboolean     ostree_repo_map_cached_remote_pack_index (OstreeRepo       *self,
                                                        const char       *remote_name,
                                                        const char       *pack_checksum,